/**
 * @projectName interview_-assistant
 * @package org.example.util
 * @className org.example.util.FileStorage
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package org.example.util;

import org.example.pojo.entity.Article;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * FileStorage
 * @description
 * @author admin
 * @date 2025/4/14 10:37
 * @version TODO
 */
@Component
public class FileStorage {

    @Value("${crawler.save.dir:./articles}")
    private String savePath;

    public void saveArticle(Article article) throws IOException {
        // 文件存储逻辑
        Path dir = Paths.get("E:\\article");
        if (!Files.exists(dir))
            Files.createDirectories(dir);

        String fileName = article.getTitle()
                .replaceAll("[\\\\/]", "_")
                .substring(0, Math.min(50, article.getTitle().length())) + ".txt";

        String content = String.format("标题：%s\n链接：%s\n\n正文：\n%s\n发布时间：%s",
                article.getTitle(), article.getUrl(), article.getContent(), article.getPublishTime());

        Files.writeString(dir.resolve(fileName), content);
    }
}
